<?php

namespace App\Admin\Controller\Rbac;

class Route extends \App\Admin\Controller\Authed {
    
    public function index() {
        
        $do = $this->request->get('do');
        
        if ($do == 'sort') {
            $sorts = $this->request->getPost('sorts');
            foreach ($sorts as $key => $value) {
                QF()->Rbac()->Route()->setSort($key, $value);
            }
        }
        
        $rows = QF()->Rbac()->Route()->getTreeArray();
        
        $this->assign('rows', $rows);
    }
    
    public function edit() {
        
        $this->id = $id = intval($this->request->get('id'));
        
        if ($this->request->isPost()) {
            
            $data = $this->request->get();
            
            if ($id) {
                $result = QF()->Rbac()->Route()->update($id, $data);
                
                if ($result) {
                    succ('', '', 2);
                } else {
                    fail();
                }
            } else {
                $result = QF()->Rbac()->Route()->add($data);
                if ($result) {
                    succ('', '', 2);
                } else {
                    fail();
                }
            }
        }
        
        $this->detail = QF()->Rbac()->Route()->get($id);
        
        $this->routes = QF()->Rbac()->Route()->getTreeArray();
        
        $this->parent_id = intval($this->request->get('parent_id'));
        
    }
    
    public function remove() {
        $id = intval($this->request->get('id'));
        $result = QF()->Rbac()->Route()->remove($id);
        if ($result) {
            succ();
        } else {
            fail();
        }
    }
    
}